package com.tianyang.yukuaifuzs.business.trade.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.SettleBankOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.WithdrawOutput;
import com.tianyang.yukuaifuzs.business.trade.contract.WithdrawContract;
import com.tianyang.yukuaifuzs.business.trade.presenter.WithdrawPresenter;
import com.tianyang.yukuaifuzs.common.cache.CacheManage;
import com.tianyang.yukuaifuzs.common.cache.CacheModel;
import com.tianyang.yukuaifuzs.utils.ToastUtil;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：提现
 * Created by xyj on 2024/3/11
 */
public class WithdrawActivity extends BaseTitleActivity implements WithdrawContract.View {

    @BindView(R.id.bankNameTv)
    TextView bankNameTv;
    @BindView(R.id.etMoney)
    EditText etMoney;
    @BindView(R.id.amountTv)
    TextView amountTv;
    @BindView(R.id.actAmountTv)
    TextView actAmountTv;
    @BindView(R.id.ketiamountTv)
    TextView ketiamountTv;

    private WithdrawContract.Presenter mPresenter;

    private WithdrawOutput mOutput = new WithdrawOutput();

    private String userName;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_withdraw;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("提现");
        mPresenter = new WithdrawPresenter(this);
        mPresenter.queryUserSettlementCard();
        mPresenter.getcantixian();
    }

    @OnClick({R.id.allWithdrawTv, R.id.confirmTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.allWithdrawTv:
                if (mOutput != null && !TextUtils.isEmpty(mOutput.getAmount())
                        && new BigDecimal(mOutput.getAmount()).floatValue() > 0) {
                    etMoney.setText(mOutput.getAmount());
                }
                break;
            case R.id.confirmTv:
                if (checkWithdrawData()) {
                    mPresenter.postixian(etMoney.getText().toString());
                }
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (output != null) {
            userName = output.getUsername();
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showWithdrawView(WithdrawOutput output) {
        if (output != null) {
            mOutput = output;
            amountTv.setText("￥" + VerifyUtil.decimalFormat(output.getAmount()));
            actAmountTv.setText("￥" + VerifyUtil.decimalFormat(output.getActAmount()));
            ketiamountTv.setText("￥" + VerifyUtil.decimalFormat(output.getKetiamount()));
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showSettlementCardView(SettleBankOutput output) {
        if (output != null) {
            bankNameTv.setText(output.getBankName() + "（" + VerifyUtil.getBankCardNumber(output.getBankCardId()) + "）");
        }
    }

    private boolean checkWithdrawData() {
        String money = etMoney.getText().toString();
        if (TextUtils.isEmpty(money)) {
            ToastUtil.show("请输入提现金额");
            return false;
        }
        if (TextUtils.isEmpty(mOutput.getAmount()) || new BigDecimal(mOutput.getAmount()).floatValue() <= 0) {
            ToastUtil.show("当前账户余额不足");
            return false;
        }
        if (new BigDecimal(money).compareTo(new BigDecimal(mOutput.getAmount())) > 0) {
            ToastUtil.show("提现金额不得大于可提现金额");
            return false;
        }
        if (new BigDecimal(money).floatValue() < 1) {
            ToastUtil.show("提现金额不得小于1元");
            return false;
        }
        if (new BigDecimal(money).floatValue() < 10) {
            if (TextUtils.equals("18307365490", userName) || TextUtils.equals("18028148540", userName)
                    || TextUtils.equals("18684979317", userName) || TextUtils.equals("13203060506", userName)) {
                return true;
            }
            ToastUtil.show("提现金额不得低于10元");
            return false;
        }
        return true;
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(WithdrawContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
